package com.androidvizlab.bachelor.customcomponents.fragments;

import com.androidvizlab.bachelor.R;
import com.androidvizlab.bachelor.customcomponents.listenersandadapters.CustomTextChangedListener;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

/**
 * Custom fragment for edit option file UI
 * Used to create both General and Advanced option fragment
 * The id of the xml file to be inflated is passed as an argument,
 * via intent bundle.
 * 
 * @author The Hive
 *
 */
public class EditOptionFileFragment extends Fragment{
	
	//xml and view inflating
	private View v; //Root view to inflate
	private int layoutID = -2; //ID of the xml file to inflate
	private Bundle bundle = null; //Bundle used to passed extra info to the intent
		
	//Layout
	private LinearLayout container = null;
	
	//Listeners
	private OnClickListener onClickListener = null;
	private OnCheckedChangeListener onCheckedChangeListener = null;
	
	//TextWatcher
	private TextWatcher textWatcher = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		//Get the layout id(of the xml) from the intent
		bundle = this.getArguments();
		
		if(bundle != null)
		{
			layoutID = bundle.getInt("layoutid",-2);
		}
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState){
		super.onActivityCreated(savedInstanceState);
	}

	/**
	 * Inflates the proper xml given the id
	 */
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		
		//Determine which xml to inflate
		if(layoutID != -2)
		{
			v = inflater.inflate(layoutID,container,false);
			
			initUIComponents(layoutID, v);
			Log.i("Fragment_view_created","view created");
		}
		
		return v;
	}
	
	//Initialises UI components
	/**
	 * Used to get a reference of the main container of the fragment and set Listeners to its child views.
	 * @param id to determine which xml file was used
	 * @param v parent view
	 */
	public void initUIComponents(int id, View v)
	{
		switch(id)
		{
		case R.layout.editsettingsgeneral:
			
			container = (LinearLayout) v.findViewById(R.id.containerEditSettingsGeneral);

			setListenerToAllChildViews(container);
			
			break;
		case R.layout.editsettingsadvanced:
			
			container = (LinearLayout) v.findViewById(R.id.containerEditSettingsAdvanced);
			
			setListenerToAllChildViews(container);
			
			break;
		}
	}
	
	/**
	 * Set listener to all child view. Loops through the parent View's children
	 * and set the proper listener provided by the Activity.
	 * Warning: all listeners must be set.
	 * 
	 * @param parent parent view
	 */
	private void setListenerToAllChildViews(ViewGroup parent)
	{
		for(int i = 0; i < parent.getChildCount(); i++)
		{
			View child = parent.getChildAt(i);
			
			if(child instanceof ViewGroup)
			{
				if(child.getClass().equals(RadioGroup.class))
				{
					((RadioGroup)child).setOnCheckedChangeListener(getOnCheckChangeListener());
				}
				else
				{
					setListenerToAllChildViews((ViewGroup)child);
				}
			}
			else if(child != null)
			{
				if(child.getClass().equals(Button.class))
				{
					((Button)child).setOnClickListener(getOnClickListener());
				}
				else if(child.getClass().equals(CheckBox.class))
				{
					((CheckBox)child).setOnClickListener(getOnClickListener());
				}
				else if(child.getClass().equals(EditText.class))
				{
					CustomTextChangedListener textWatcher = new CustomTextChangedListener();
					textWatcher.setEditText((EditText)child);
					((EditText)child).addTextChangedListener(textWatcher);
					//((EditText)child).addTextChangedListener(getTextWatcher());
				}
				else if(child.getClass().equals(ImageButton.class))
				{
					((ImageButton)child).setOnClickListener(getOnClickListener());
				}
			}
		}
	}
	
	
	//*** GETTERS AND SETTERS ***//
	
	public OnClickListener getOnClickListener() {
		return onClickListener;
	}

	public void setOnClickListener(OnClickListener onClickListener) {
		this.onClickListener = onClickListener;
	}
	
	public OnCheckedChangeListener getOnCheckChangeListener()
	{
		return onCheckedChangeListener;
	}
	
	public void setOnCheckedChangeListener(OnCheckedChangeListener onCheckedChangeListener) {
		this.onCheckedChangeListener = onCheckedChangeListener;
	}
	
	public TextWatcher getTextWatcher() {
		return textWatcher;
	}

	public void setTextWatcher(TextWatcher textWatcher) {
		this.textWatcher = textWatcher;
	}
}
